﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace AnakraEditor7
{
    class Button
    {
        Vector2 position;
        Texture2D texture;

        public Button(Texture2D Tex, Vector2 Pos)
        {
            texture = Tex;
            position = Pos;
        }
        public bool IsDown()
        {
            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                if (new Rectangle((int)position.X, (int)position.Y, (int)texture.Width, (int)texture.Height).Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y)))
                {
                    return true;
                }
            }
            return false;
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);
        }
    }
    class TextBox
    {
        public Vector2 position;
        public Texture2D texture;
        public String text;
        public SpriteFont font;
        String textToAdd;
        bool[] keysLastFrame;
        Keys currentKey;
        int currentNum = 0;
        int time = 0;
        int time2 = 0;
        int i = 0;
        int tiles = 1;
        int maxChars;

        public TextBox(Texture2D Tex, Vector2 Pos, SpriteFont Font, int XTiling, int maxCharacters)
        {
            texture = Tex;
            maxChars = maxCharacters;
            position = Pos;
            font = Font;
            text = "";
            textToAdd = "";
            keysLastFrame = new bool[34];
            currentNum = 0;
            tiles = XTiling;
        }

        public TextBox(Texture2D Tex, Vector2 Pos, SpriteFont Font, int maxCharacters)
        {
            texture = Tex;
            maxChars = maxCharacters;
            position = Pos;
            font = Font;
            text = "";
            textToAdd = "";
            keysLastFrame = new bool[34];
            currentNum = 0;
        }

        public void Update()
        {
            currentKey = Keys.A;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.B;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.C;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.D;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.E;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.F;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.G;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.H;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.I;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.J;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.K;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.L;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.M;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.N;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.O;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.P;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.Q;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.R;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.S;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.T;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.U;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.V;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.W;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.X;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.Y;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.Z;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = currentKey.ToString();
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.OemPeriod;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = ".";
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.OemQuestion;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                if ((Keyboard.GetState().IsKeyDown(Keys.RightShift) || Keyboard.GetState().IsKeyDown(Keys.LeftShift)))
                {
                    textToAdd = "?";
                }
                else
                {
                    textToAdd = "/";
                }
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.OemComma;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = ",";
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.OemBackslash;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = "\\";
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.OemSemicolon;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                if ((Keyboard.GetState().IsKeyDown(Keys.RightShift) || Keyboard.GetState().IsKeyDown(Keys.LeftShift)))
                {
                    textToAdd = ":";
                }
                else
                {
                    textToAdd = ";";
                }
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            currentNum++;
            currentKey = Keys.OemQuotes;
            if (Keyboard.GetState().IsKeyDown(currentKey) && !keysLastFrame[currentNum])
            {
                textToAdd = "'";
                textToAdd = textToAdd.ToLower();
                keysLastFrame[currentNum] = true;
            }
            if (Keyboard.GetState().IsKeyUp(currentKey))
            {
                keysLastFrame[currentNum] = false;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.RightShift) || Keyboard.GetState().IsKeyDown(Keys.LeftShift))
            {
                textToAdd = textToAdd.ToUpper();
                if (textToAdd == "'")
                {
                    textToAdd = "\"";
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Space) && time > 20)
            {
                textToAdd = " ";
                time = 0;
            }
            time++;
            currentNum = 0;
            if (text.Length <= maxChars)
            {
                text += textToAdd;
            }
            textToAdd = "";
            if (Keyboard.GetState().IsKeyDown(Keys.Back) && time2 > 10)
            {
                if (text.Length >= 1)
                {
                    text = text.Remove(text.Length - 1, 1);
                }
                time2 = 0;
            }
            time2++;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            i = 0;
            while (i <= tiles)
            {
                spriteBatch.Draw(texture, new Vector2(position.X + (texture.Width * i), position.Y), null, Color.White, 0.0f, new Vector2(0,0), 1.0f, SpriteEffects.None, 1.0f);
                i++;
            }
            spriteBatch.DrawString(font, text, position, Color.Black, 0.0f, new Vector2(0,0), 1.0f, SpriteEffects.None, 0.99f);
        }
    }
}
